﻿using Company.Core.Ioc;
using Company.Application.Share.Events;
using Company.Application.Share.Model;
using Company.Application.Share.Prism;
using Prism.Commands;
using Prism.Events;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;

namespace Shell.ViewModels
{

    internal class MainWindowViewModel:BindableBase
    {
        public ICommand LoadedCommand { get;}
        public IModuleManager ModuleManager { get; }
        public IRegionManager RegionManager { get; }
        public MainWindowViewModel(IModuleManager moduleManager,IRegionManager regionManager) 
        {
            ModuleManager=moduleManager;
            RegionManager=regionManager;
            LoadedCommand = new DelegateCommand(Loaded);


        }

        private void Loaded()
        {
            IRegion mainRegion = PrismProvider.RegionManager.Regions[RegionNames.MainRegion];
            mainRegion.NavigationService.Navigated += NavigationService_Navigated;
            PrismProvider.ModuleManager.LoadModule(ModuleNames.ApplicationLoginModule);
            PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.LoginView);
            PrismProvider.EventAggregator.GetEvent<LoginSuccessEvent>().Subscribe(OnLogined,ThreadOption.UIThread);
        }

        private void OnLogined(CurrentUser user)
        {
           
            PrismProvider.ModuleManager.LoadModule(ModuleNames.ApplicationMainModule);
            PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion,ViewNames.MainView);


        }
        private Window MainWindow { get; set; }=Application.Current.MainWindow;
        private void NavigationService_Navigated(object sender, RegionNavigationEventArgs e)
        {
            switch (e.Uri.OriginalString)
            {
                case ViewNames.LoginView:
                    //MainWindow.ResizeMode=ResizeMode.NoResize;
                    //MainWindow.SizeToContent = SizeToContent.WidthAndHeight;
                    //MainWindow.WindowState = WindowState.Normal;

                    //MainWindow.Width = 600;
                    //MainWindow.Height = 350;

                    //MainWindow.WindowStyle = WindowStyle.None; 
                    //break;
                case ViewNames.MainView:
                    MainWindow.Width = 1920;
                    MainWindow.Height = 1080;
                    MainWindow.ResizeMode=ResizeMode.CanResize;
                    MainWindow.SizeToContent=SizeToContent.Manual;
                    MainWindow.WindowState=WindowState.Maximized;
                    MainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
                    break;
                default:
                    break;
            }
        }

        private string title="重庆教主上位机开发VIP课程";
        public string Title 
        {  
            get { return title; } 
            set { title = value;RaisePropertyChanged(); }
        }
    }
}
